<?php

class KhuyenMai extends CI_Controller {

    function index() {
        $this->load->model('khuyenmai_model');
        $this->load->model('khachsan_model');
        $data['country'] = $this->khachsan_model->layQuocGia();
        $data['Tinh_Thanh'] = $this->khuyenmai_model->LayTatCaTinhThanhTrongNuoc();
        $data['QuocGia'] = $this->khuyenmai_model->LayTatCaQuocGia();

        $this->load->view('home/khuyenmai_view', $data);
    }

    function LayTatCaKM() {
        $this->load->model('khuyenmai_model');
        $this->load->model('khachsan_model');
        $data['country'] = $this->khachsan_model->layQuocGia();
        $result = $this->khuyenmai_model->LayTatCaKhuyenMaiConHan();
        $data = "";
        foreach ($result as $row) {

            $MaKS = $row->MaKS;
            $data .="<div id=" . $MaKS . " class='amazing_detail'>";
            $images = $row->images;
            if (!$images) {
                $images = "images/no_image.jpg";
            }
            $data .="<div class='hotel_content'><a href='" . site_url('datphong/khachsan/' . $MaKS) . "'><image class='imagehotel' src=" . $images . "></a>";
            $noidungKM = $row->NoiDung;
            $data .= "<div class='promotioncontent'><p style='color:white;'><b>" . $noidungKM . "</b></p></div>";
            $NgayHetHan = date("d/m/Y", strtotime($row->NgayKetThuc));
            ;
            $data .= "<div class='promotiondeadline'><p style='color:white;'><b>Thời hạn KM: " . $NgayHetHan . "</b></p></div>";
            $TenKS = $row->TenKS;
            $gioihan = 25;
            if (strlen($TenKS) <= $gioihan) {
                $TenKS = $TenKS;
            } else {
                if (strpos($TenKS, " ", $gioihan) > $gioihan) {
                    $new_gioihan = strpos($TenKS, " ", $gioihan);
                    $new_chuoi = substr($TenKS, 0, $new_gioihan) . "...";
                    $TenKS = $new_chuoi;
                }
                $new_chuoi = substr($TenKS, 0, $gioihan) . "...";
                $TenKS = $new_chuoi;
            }
            $data .="<div class='hotel_info'><div class='amazinging-name'><a href='" . site_url('datphong/khachsan/' . $MaKS) . "' title='" . $TenKS . "'><b>" . $TenKS . "</b></a><br></div>";
            $tieuchuanKS = $row->TieuChuan;
            $data .="<img src='resources/home/images/template/star" . $tieuchuanKS . ".0.png'>";
            $quan = $row->Quan;
            $thanhpho = $row->Tinh_ThanhPho;
            $quocgia = $row->QuocGia;
            $data .="<div class='hotel-address'><span style='color: #666666;font-size:11px;'>" . $quan . ", " . $thanhpho . "</span></div></div>";
            $tileKM = $row->TileKM;
            $GiaBanDau = $row->Gia;
            $GiaMoi = round($GiaBanDau * (1 - $tileKM / 100), 0) * 1000;
            $GiaBanDau = number_format($GiaBanDau * 1000, 0, ',', '.');
            $GiaMoi = number_format($GiaMoi, 0, ',', '.');
            $data .="<table width='100%'><tbody><tr><td width='71%'><p class='hotel_content_rate'><b class='price_public'>" . $GiaMoi . " VNĐ</b><br>";
            $data .="<sub class='sub_discount'>" . $GiaBanDau . " VNĐ</sub></p></td><td><div><a href=" . site_url('datphong/khachsan/' . $MaKS) . " class='book_amazing'><b>Chọn</b></a></div></td></tr></tbody></table></div></div>";
        }

        echo $data;
    }

    function LayKMTheoQuocGia() {
        $this->load->model('khachsan_model');
        $data['country'] = $this->khachsan_model->layQuocGia();
        if (isset($_POST['ten_quocgia'])) {
            $ten_quocgia = $_POST['ten_quocgia'];
            $this->load->model('khuyenmai_model');

            $result = $this->khuyenmai_model->LayKMTheoQuocGia($ten_quocgia);
            $data = "";
            foreach ($result as $row) {

                $MaKS = $row->MaKS;
                $data .="<div id=" . $MaKS . " class='amazing_detail'>";
                $images = $row->images;
                if (!$images) {
                    $images = "images/no_image.jpg";
                }
                $data .="<div class='hotel_content'><a href='" . site_url('datphong/khachsan/' . $MaKS) . "'><image class='imagehotel' src=" . $images . "></a>";
                $noidungKM = $row->NoiDung;
                $data .= "<div class='promotioncontent'><p style='color:white;'><b>" . $noidungKM . "</b></p></div>";
                $NgayHetHan = date("d/m/Y", strtotime($row->NgayKetThuc));
                ;
                $data .= "<div class='promotiondeadline'><p style='color:white;'><b>Thời hạn KM: " . $NgayHetHan . "</b></p></div>";
                $TenKS = $row->TenKS;
                $gioihan = 27;
                if (strlen($TenKS) <= $gioihan) {
                    $TenKS = $TenKS;
                } else {
                    if (strpos($TenKS, " ", $gioihan) > $gioihan) {
                        $new_gioihan = strpos($TenKS, " ", $gioihan);
                        $new_chuoi = substr($TenKS, 0, $new_gioihan) . "...";
                        $TenKS = $new_chuoi;
                    }
                    $new_chuoi = substr($TenKS, 0, $gioihan) . "...";
                    $TenKS = $new_chuoi;
                }
                $data .="<div class='hotel_info'><div class='amazinging-name'><a href='" . site_url('datphong/khachsan/' . $MaKS) . "' title='" . $TenKS . "'><b>" . $TenKS . "</b></a><br></div>";
                $tieuchuanKS = $row->TieuChuan;
                $data .="<img src='resources/home/images/template/star" . $tieuchuanKS . ".0.png'>";
                $quan = $row->Quan;
                $thanhpho = $row->Tinh_ThanhPho;
                $quocgia = $row->QuocGia;
                $data .="<div class='hotel-address'><span style='color: #666666;font-size:11px;'>" . $quan . ", " . $thanhpho . "</span></div></div>";
                $tileKM = $row->TileKM;
                $GiaBanDau = $row->Gia;
                $GiaMoi = round($GiaBanDau * (1 - $tileKM / 100), 0) * 1000;
                $GiaBanDau = number_format($GiaBanDau * 1000, 0, ',', '.');
                $GiaMoi = number_format($GiaMoi, 0, ',', '.');
                $data .="<table width='100%'><tbody><tr><td width='71%'><p class='hotel_content_rate'><b class='price_public'>" . $GiaMoi . " VNĐ</b><br>";
                $data .="<sub class='sub_discount'>" . $GiaBanDau . " VNĐ</sub></p></td><td><div><a href=" . site_url('datphong/khachsan/' . $MaKS) . " class='book_amazing'><b>Chọn</b></a></div></td></tr></tbody></table></div></div>";
            }

            echo $data;
        }
    }

    function LayKMTheoTinhThanhTrongNuoc() {
        $this->load->model('khachsan_model');
        $data['country'] = $this->khachsan_model->layQuocGia();
        if (isset($_POST['ten_tinh'])) {
            $ten_tinh = $_POST['ten_tinh'];
            $this->load->model('khuyenmai_model');

            $result = $this->khuyenmai_model->LayKMTheoTinhThanhTrongNuoc($ten_tinh);
            //con phan xet ngay ket thuc
            $data = "";
            foreach ($result as $row) {

                $MaKS = $row->MaKS;
                $data .="<div id=" . $MaKS . " class='amazing_detail'>";
                $images = $row->images;
                if (!$images) {
                    $images = "images/no_image.jpg";
                }
                $data .="<div class='hotel_content'><a href='" . site_url('datphong/khachsan/' . $MaKS) . "'><image class='imagehotel' src=" . $images . "></a>";
                $noidungKM = $row->NoiDung;
                $data .= "<div class='promotioncontent'><p style='color:white;'><b>" . $noidungKM . "</b></p></div>";
                $NgayHetHan = date("d/m/Y", strtotime($row->NgayKetThuc));
                ;
                $data .= "<div class='promotiondeadline'><p style='color:white;'><b>Thời hạn KM: " . $NgayHetHan . "</b></p></div>";
                $TenKS = $row->TenKS;
                $gioihan = 27;
                if (strlen($TenKS) <= $gioihan) {
                    $TenKS = $TenKS;
                } else {
                    if (strpos($TenKS, " ", $gioihan) > $gioihan) {
                        $new_gioihan = strpos($TenKS, " ", $gioihan);
                        $new_chuoi = substr($TenKS, 0, $new_gioihan) . "...";
                        $TenKS = $new_chuoi;
                    }
                    $new_chuoi = substr($TenKS, 0, $gioihan) . "...";
                    $TenKS = $new_chuoi;
                }
                $data .="<div class='hotel_info'><div class='amazinging-name'><a href='" . site_url('datphong/khachsan/' . $MaKS) . "' title='" . $TenKS . "'><b>" . $TenKS . "</b></a><br></div>";
                $tieuchuanKS = $row->TieuChuan;
                $data .="<img src='resources/home/images/template/star" . $tieuchuanKS . ".0.png'>";
                $quan = $row->Quan;
                $thanhpho = $row->Tinh_ThanhPho;
                $quocgia = $row->QuocGia;
                $data .="<div class='hotel-address'><span style='color: #666666;font-size:11px;'>" . $quan . ", " . $thanhpho . "</span></div></div>";
                $tileKM = $row->TileKM;
                $GiaBanDau = $row->Gia;
                $GiaMoi = round($GiaBanDau * (1 - $tileKM / 100), 0) * 1000;
                $GiaBanDau = number_format($GiaBanDau * 1000, 0, ',', '.');
                $GiaMoi = number_format($GiaMoi, 0, ',', '.');
                $data .="<table width='100%'><tbody><tr><td width='71%'><p class='hotel_content_rate'><b class='price_public'>" . $GiaMoi . " VNĐ</b><br>";
                $data .="<sub class='sub_discount'>" . $GiaBanDau . " VNĐ</sub></p></td><td><div><a href=" . site_url('datphong/khachsan/' . $MaKS) . " class='book_amazing'><b>Chọn</b></a></div></td></tr></tbody></table></div></div>";
            }

            echo $data;
        }
    }

}

?>